package controller

import (
	"github.com/gin-gonic/gin"

	"gitee.com/csingo/cConfig"
	"gitee.com/csingo/cError"
	"gitee.com/csingo/cResponse"
	"gitee.com/csingo/cServer"

	"gitee.com/csingo/example/app/demo/service"
	"gitee.com/csingo/example/base/demo"
)

type HelloController struct{}

func (i *HelloController) ControllerName() (app, name string) {
	return "demo", "HelloController"
}

func (i *HelloController) Ping(ctx *gin.Context) (err error) {
	data := map[string]any{
		"project": "gitee.com/csingo/example",
		"routes":  cServer.Routes(),
	}
	return cResponse.Api(ctx, data, nil)
}

func (i *HelloController) Version(ctx *gin.Context) (err error) {
	version := cServer.GetVersion()
	return cResponse.Text(ctx, version, err)
}

func (i *HelloController) Config(ctx *gin.Context) (err error) {
	key := ctx.Query("key")
	config, err := cConfig.GetConf(key)
	return cResponse.Api(ctx, config, err)
}

func (i *HelloController) Hello(ctx *gin.Context) (err error) {
	req := &demo.HelloServiceHelloReq{}
	err = ctx.ShouldBind(req)
	if err != nil {
		return cResponse.Api(ctx, nil, cError.CError_Csingo_Param.Merge(err))
	}

	result, err := (&service.HelloService{}).Hello(ctx, req)

	return cResponse.Api(ctx, result, err)
}
